package cn.aihongbo.kmp.pages.order

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.GridItemSpan
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.Divider
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import cn.aihongbo.kmp.data.bean.CitiesBaseDiy
import cn.aihongbo.kmp.data.bean.CityBean
import cn.aihongbo.kmp.data.bean.CountriesVosBean
import cn.aihongbo.kmp.data.repository.CityRepository
import cn.aihongbo.kmp.data.vm.CityViewModel
import coil3.compose.AsyncImage
import kotlinproject.composeapp.generated.resources.Res
import kotlinproject.composeapp.generated.resources.h_default_back
import kotlinproject.composeapp.generated.resources.h_default_search
import org.jetbrains.compose.resources.painterResource

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun ChooseCityView(chooseCallback: (cityBean: CityBean?) -> Unit) {

    val viewModel = viewModel { CityViewModel(CityRepository()) }
    val inputTxt = remember { mutableStateOf("") }
    val selectedDiy = remember { mutableStateOf<CitiesBaseDiy?>(null) }
    val countriesVos = remember { mutableStateListOf<CountriesVosBean?>() }

    LaunchedEffect(Unit) {
        viewModel.loadCitiesByCountry()
        viewModel.loadBaseDataDiy()
        viewModel.hotCities.let {
            countriesVos.add(CountriesVosBean(citiesVos = it))
        }
    }

    Column(modifier = Modifier.height(688.dp)) {
        Row(modifier = Modifier.fillMaxWidth().height(60.dp)) {
            Image(
                painter = painterResource(Res.drawable.h_default_back),
                contentDescription = null,
                modifier = Modifier.align(
                    Alignment.CenterVertically
                ).width(42.dp)
            )

            Row(
                modifier = Modifier.padding(top = 12.dp, bottom = 12.dp, end = 16.dp).fillMaxWidth()
                    .background(Color(0xFFF5F5F5), shape = RoundedCornerShape(100))

            ) {
                Image(
                    painter = painterResource(Res.drawable.h_default_search),
                    contentDescription = null,
                    modifier = Modifier.padding(start = 10.dp).align(
                        Alignment.CenterVertically
                    )
                )
                Box(modifier = Modifier.fillMaxSize()) {
                    BasicTextField(
                        value = inputTxt.value,
                        onValueChange = {
                            inputTxt.value = it
                            viewModel.searchCities(it)
                        },
                        textStyle = TextStyle(
                            fontSize = 13.sp
                        ),
                        singleLine = true,
                        maxLines = 1,
                        modifier = Modifier.align(Alignment.CenterStart)
                    )
                    if (inputTxt.value.isEmpty()) {
                        Text(
                            "想去哪里玩",
                            color = Color(0xFF989898),
                            fontSize = 13.sp,
                            modifier = Modifier.align(Alignment.CenterStart)
                        )
                    }
                }
            }
        }

        Box() {
            Row(modifier = Modifier.fillMaxHeight()) {
                LazyColumn(
                    modifier = Modifier.width(100.dp).fillMaxHeight().background(Color(0xFFF4F6F8))
                ) {
                    item {
                        Box(
                            modifier = Modifier.fillMaxWidth().height(55.dp)
                                .align(Alignment.CenterVertically)
                                .background(if (selectedDiy.value == null) Color.White else Color.Transparent)
                                .clickable {
                                    selectedDiy.value = null
                                    countriesVos.clear()
                                    viewModel.hotCities.let {
                                        countriesVos.add(CountriesVosBean(citiesVos = it))
                                    }
                                }) {
                            Text(
                                "热门推荐", modifier = Modifier.align(Alignment.Center),
                                fontSize = 14.sp,
                                color = if (selectedDiy.value == null) Color.Black else Color(
                                    0xFF979797
                                ),
                            )

                            if (selectedDiy.value == null) {
                                Box(
                                    modifier = Modifier.width(4.dp).height(40.dp)
                                        .align(Alignment.CenterStart).background(
                                            Color(0xFFFF4F4F), shape = RoundedCornerShape(
                                                topEnd = 100f, bottomEnd = 100f
                                            )
                                        ),
                                )
                            }
                        }

                    }
                    for (item in viewModel.citiesBaseDiy) item {
                        Box(
                            modifier = Modifier.fillMaxWidth().height(55.dp)
                                .align(Alignment.CenterVertically)
                                .background(if (selectedDiy.value?.continentId == item?.continentId) Color.White else Color.Transparent)
                                .clickable {
                                    selectedDiy.value = item
                                    item?.countriesVos?.let {
                                        countriesVos.clear()
                                        countriesVos.addAll(it)
                                    }
                                }) {
                            Text(
                                text = "${item?.continentName}",
                                fontSize = 14.sp,
                                color = if (selectedDiy.value?.continentId == item?.continentId) Color.Black else Color(
                                    0xFF979797
                                ),
                                modifier = Modifier.align(Alignment.Center)
                            )

                            if (selectedDiy.value?.continentId == item?.continentId) {
                                Box(
                                    modifier = Modifier.width(4.dp).height(40.dp)
                                        .align(Alignment.CenterStart).background(
                                            Color(0xFFFF4F4F), shape = RoundedCornerShape(
                                                topEnd = 100f, bottomEnd = 100f
                                            )
                                        ),
                                )
                            }
                        }
                    }
                }
                LazyVerticalGrid(columns = GridCells.Fixed(2)) {
                    for (country in countriesVos) {
                        if (country?.countryId != null) {
                            item(span = { GridItemSpan(2) }) {
                                Box(
                                    modifier = Modifier.padding(6.dp).background(
                                        Color(0xffFAFAFA), shape = RoundedCornerShape(12.dp)
                                    ).height(130.dp)
                                ) {
                                    AsyncImage(
                                        model = country.imgUrl,
                                        contentDescription = null,
                                        contentScale = ContentScale.FillBounds,
                                        modifier = Modifier.fillMaxSize().clip(
                                            RoundedCornerShape(12.dp)
                                        )
                                    )
                                    Box(
                                        modifier = Modifier.fillMaxSize().background(
                                            Color(0x33000000), shape = RoundedCornerShape(12.dp)
                                        ).padding(12.dp)
                                    ) {
                                        Text(
                                            "${country.countryName}",
                                            color = Color.White,
                                            fontSize = 14.sp
                                        )
                                        Text(
                                            "${country.countryNameEn}",
                                            color = Color.White,
                                            fontSize = 11.sp,
                                            modifier = Modifier.padding(top = 16.dp)
                                        )
                                    }
                                }
                            }
                        }

                        country?.citiesVos?.forEach { cities ->
                            item {
                                Box(
                                    modifier = Modifier.padding(6.dp).background(
                                        Color(0xffFAFAFA), shape = RoundedCornerShape(12.dp)
                                    ).height(130.dp).clickable {
                                        chooseCallback.invoke(cities)
                                    }
                                ) {
                                    AsyncImage(
                                        model = cities?.imgUrl,
                                        contentDescription = null,
                                        contentScale = ContentScale.FillBounds,
                                        modifier = Modifier.fillMaxSize()
                                            .clip(RoundedCornerShape(12.dp))
                                    )
                                    Box(
                                        modifier = Modifier.fillMaxSize().background(
                                            Color(0x33000000), shape = RoundedCornerShape(12.dp)
                                        ).padding(12.dp)
                                    ) {
                                        Text(
                                            "${cities?.cityName}",
                                            color = Color.White,
                                            fontSize = 14.sp
                                        )
                                        Text(
                                            "${cities?.cityNameEn}",
                                            color = Color.White,
                                            fontSize = 11.sp,
                                            modifier = Modifier.padding(top = 16.dp)
                                        )
                                    }
                                }
                            }
                        }

                    }
                }
            }
            SearchView(viewModel, chooseCallback)
        }
    }
}

@Composable
fun SearchView(viewModel: CityViewModel, chooseCallback: (cityBean: CityBean?) -> Unit) {
    if (viewModel.searchCities.isNotEmpty()) {
        Box(modifier = Modifier.fillMaxSize().background(Color.White)) {
            LazyColumn {
                viewModel.searchCities.forEach {
                    item {
                        Row(Modifier.height(40.dp).fillMaxWidth().padding(start = 18.dp).clickable {
                            chooseCallback.invoke(it)
                        }) {
                            Text(
                                "${it?.cityName}",
                                fontSize = 16.sp,
                                fontWeight = FontWeight.Bold,
                                color = Color(0xff111111),
                                modifier = Modifier.align(Alignment.CenterVertically)
                            )
                            Text(
                                "${it?.countryName}",
                                fontSize = 13.sp,
                                color = Color(0xFF979797),
                                modifier = Modifier.align(Alignment.CenterVertically)
                                    .padding(start = 8.dp)
                            )
                        }
                        Divider()
                    }
                }
            }
        }
    }
}